package com.onesports.intelligent.k12.polarlight.service.course;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.intelligent.k12.polarlight.common.utils.Assert;
import com.onesports.intelligent.k12.polarlight.domain.dto.course.ApprovalDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.CourseAdjustPriceRecord;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.CourseSellPriceRel;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.CourseAdjustPriceRecordVO;
import com.onesports.intelligent.k12.polarlight.enums.AuditStatusEnum;
import com.onesports.intelligent.k12.polarlight.mapper.course.CourseAdjustPriceRecordMapper;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Wsy
 **/
@Service
@RequiredArgsConstructor
public class AdjustPriceRecordService extends ServiceImpl<CourseAdjustPriceRecordMapper, CourseAdjustPriceRecord> {

    private final CourseAdjustPriceRecordMapper mapper;
    private final CourseSellPriceRelService courseSellPriceRelService;


    /**
     * 调价记录
     *
     * @param page                分页参数
     * @param applyOrganizationId 申请机构id
     * @param campusId            校区id
     * @param courseId            课程id
     * @param auditStatus         审核状态
     * @param auditBy             审核人
     * @return IPage<CourseAdjustPriceRecordVO>
     */
    public IPage<CourseAdjustPriceRecordVO> adjustRecord(Page<Object> page, String applyOrganizationId,
                                                         String campusId, String courseId, String auditStatus, String auditBy,
                                                         String typeName) {
        return mapper.adjustRecord(page, applyOrganizationId, campusId, courseId, auditStatus, auditBy,typeName);
    }

    /**
     * 审批
     *
     * @param dto 入参
     */
    @Transactional(rollbackFor = Exception.class)
    public void toApproval(ApprovalDTO dto) {
        //校验数据是否存在
        CourseAdjustPriceRecord record = this.mapper.selectById(dto.getId());
        Assert.notNull(record, "调价申请记录不存在");
        record.setAuditStatus(dto.getAuditStatus()).setAuditInfo(dto.getAuditInfo())
                .setAuditBy(SecurityUtils.getNickname()).setAuditTime(new Date());
        this.updateById(record);
        if (dto.getAuditStatus().equals(AuditStatusEnum.PASS.getValue())) {
            //更改售价
            CourseSellPriceRel priceRel = new CourseSellPriceRel();
            priceRel.setId(record.getCourseSellPriceRelId());
            priceRel.setPrice(record.getAdjustPrice());
            priceRel.setNewRecordId(record.getId());
            this.courseSellPriceRelService.updateById(priceRel);
        }
    }
}
